package org.zero.concurrency.demo.example.singleton;

import org.zero.concurrency.demo.annotations.ThreadSafe;

/**
 * 静态块实现的饿汉式单例-线程安全
 * 实例在类加载的时候创建
 *
 * @author 01
 */
@ThreadSafe
public class SingletonExample5 {

    /**
     * 单例对象
     */
    private volatile static SingletonExample5 instance;

    /**
     * 私有构造函数
     */
    private SingletonExample5() {
    }

    // 使用静态块初始化实例对象
    static {
        instance = new SingletonExample5();
    }

    /**
     * 静态工厂方法-获取实例
     *
     * @return instance
     */
    public static SingletonExample5 getInstance() {
        return instance;
    }

    public static void main(String[] args) {
        System.out.println(getInstance());
        System.out.println(getInstance());
    }
}
